package servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import dao.MessagesDao;
import dao.impl.MessagesDaoImpl;
import entity.Messages;
import entity.other.PageInfo;
import entity.vo.MessageData;
import utils.GetFieldUtils;
import utils.ResponseResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "MessageServlet", urlPatterns = "/bigProjects/MessageServlet")

public class MessageServlet extends HttpServlet {

    //获取全部页数
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        MessagesDao messagesDao = new MessagesDaoImpl();
        // 设置响应的内容类型为 JSON
        resp.setContentType("application/json");
        //设置ContentType字段值
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        Integer allPage = messagesDao.getAllPage();
        resp.getWriter().write(ResponseResult.success(allPage).toString());
    }

    //用户获取留言信息
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        MessagesDao messagesDao = new MessagesDaoImpl();
        // 设置响应的内容类型为 JSON
        resp.setContentType("application/json");
        //设置ContentType字段值
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String str = GetFieldUtils.getRequestPayload(req);
        ObjectMapper mapper = new ObjectMapper();
        PageInfo pageInfo = mapper.readValue(str, PageInfo.class);

        List<MessageData> messageData = messagesDao.selectPublicMessagePage(pageInfo.getPageNumber());
        resp.getWriter().write(ResponseResult.success(messageData).toString());
    }

    //新增留言
    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        MessagesDao messagesDao = new MessagesDaoImpl();
        resp.setContentType("application/json");
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");

        String mes = GetFieldUtils.getRequestPayload(req);
        ObjectMapper mapper = new ObjectMapper();
        //读取前端传递的Json字符串,转成Users对象
        Messages messages = mapper.readValue(mes, Messages.class);
        boolean b = messagesDao.insertMessage(messages);
        if (b) resp.getWriter().write(ResponseResult.success().toString());
        else resp.getWriter().write(ResponseResult.failure("插入失败").toString());

    }

    //删除留言
    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        MessagesDao messagesDao = new MessagesDaoImpl();
        resp.setContentType("application/json");
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");

        int id = Integer.parseInt(req.getParameter("id"));
        boolean b = messagesDao.deleteById(id);
        if (b) resp.getWriter().write(ResponseResult.success().toString());
        else resp.getWriter().write(ResponseResult.failure("删除失败").toString());

    }
}
